from django.contrib import auth
from django.http import HttpResponseRedirect
from django.shortcuts import render_to_response
from django import forms
from django.contrib.auth.forms import UserCreationForm

def login_view(request):
    username = request.POST.get('username',"")
    password = request.POST.get('password',"")
    user = auth.authenticate(username=username, password=password)
    if user is not None and user.is_active:
        auth.login(request, user)
        return HttpResponseRedirect("/accounts/loggedin/")
        #return HttpResponseRedirect("/accounts/profile/")
    else:
        return HttpResponseRedirect("/accounts/invalid/")

def logout_view(request):
    auth.logout(request)
    return HttpResponseRedirect("/accounts/loggedout/")

def profile(request):
    return render_to_response('accounts/profile.html')

def register(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            new_user = form.save()
            return HttpResponseRedirect("/accounts/profile/")
    else:
        form = UserCreationForm()
    return render_to_response("accounts/register.html",{
        'form':form,})
